<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$show5N = getShow35($gametype);
unset($chk);
$tbl = $g_gametype_3d[$gametype]['en'];
$tblName = $g_gametype_3d[$gametype]['ch'];
$showPage = true;
require_once( "class_3d/RateConfig_3d.php" );
require_once( "class_3d/SRate_3d.php" );

$order = getvar("order");
$split = getvar("split");
$disk = intval(getvar("disk"));
$ratemod = getvar("ratemod");
$playtype = getvar("playtype");
$lotto_id_3d = getvar("lotto_id_3d");
$showSubid = getvar("subid");
if ($lotto_id_3d == "")
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type:text/plain");
    echo "{\"errormsg\":\"Error Round\"}";
    $showPage = false;
    exit();
}
else if ($lotto_id_3d != 0)
{
    $lotto_id_3d = intval($lotto_id_3d);
}
if ($showPage)
{
    $RateConfig_3d = new RateConfig_3d($gametype);
    $SRate_3d = new SRate_3d($gametype);
    if (!isset($curruser["disk{$disk}"]) || $curruser["disk{$disk}"] == 0)
    {
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type:text/plain");
        echo "{\"errormsg\":\"Error Disk\"}";
        exit();
    }
    $split = isset($RateConfig_3d->splitArr[$split]) ? $split : 0;
    $diskStr = isset($RateConfig_3d->diskArr[$disk]) ? $disk : 0;
    $order = isset($RateConfig_3d->orderArr[$order]) ? $order : 0;
    $ratemod = isset($RateConfig_3d->ratemodArr[$ratemod]) ? $ratemod : 2;
    $playtype = isset($RateConfig_3d->playtypeArr[$playtype]) ? $playtype : "r1";
    $showSubid = isset($RateConfig_3d->subArr[$showSubid]) ? $showSubid : "0";
    if ($disk == 0)
    {
        $diskStr = "a";
    }
    else
    {
        $diskStr = $RateConfig_3d->diskArr[$disk];
    }
    $sObj = NULL;
    $rateArr = NULL;
    $tempStr = "";
    if ($show5N && $playtype == "r1p")
    {
        $showType = intval(getvar("showType"));
        $playArr = $RateConfig_3d->getLittlePageArr($showType);
    }
    else if ($playtype != "r2a")
    {
        $playArr = $RateConfig_3d->playGroupArr[$playtype];
    }
    else
    {
        $startI = intval(getvar("r2aType"));
        $playArr = array();
        $i = $startI;
        while ($i < 9)
        {
            $playArr[] = $RateConfig_3d->playGroupArr[$playtype][$i];
            $i = $i + 3;
        }
    }
    $showAllSubArr = $RateConfig_3d->showAllSubArr;

    $tempStr = "{";
    foreach ($playArr as $val)
    {
        if ($show5N && in_array($val, $g_n3n5Arr))
        {
            if (in_array($playtype, $showAllSubArr))
            {
                $subStart = $showSubid;
                $subEnd = $showSubid + 1;
            }
            else
            {
                $subStart = 0;
                $subEnd = 3;
            }
        }
        else
        {
            $subStart = 0;
            $subEnd = 1;
        }
        $subid = $subStart;
        while ($subid < $subEnd)
        {
            $tempRateBaseArr = $SRate_3d->getRates($lotto_id_3d, $disk, $val, $subid);

            if ($tempRateBaseArr == false)
            {
                $tempRateBaseArr = array();
                $i = 0;
                while ($i < 22)
                {
                    $tempIdx = "r" . str_pad($i, 2, "0", STR_PAD_LEFT);
                    $tempRateBaseArr[$tempIdx] = 0;
                    ++$i;
                }
            }
            $user_id = $SRate_3d->getAcl5ID($curruser['id'], $curruser['acl']);
            if ($tempRateBaseArr2 = $SRate_3d->getRatesS($lotto_id_3d, $val, $user_id, $subid))
            {
                $tempRateArr = array();
                foreach ($tempRateBaseArr as $key2 => $val2)
                {
                    $tempRateArr[$key2] = $tempRateBaseArr[$key2] - $tempRateBaseArr2[$key2];
                }
                $rateAllArr = $tempRateArr;
            }
            else
            {
                $rateAllArr = $tempRateBaseArr;
            }
            $numAllArr = $RateConfig_3d->getBetTypeNumber($val);

            $rateOKArr = array();
            if (isset($SRate_3d->betForNumDiffTbl[$val]))
            {
                $getRatesSNumArr = $SRate_3d->getRatesSNumArr($lotto_id_3d, $user_id, $val, $subid);
                $getRatesNumArr = $SRate_3d->getRatesNumArr($lotto_id_3d, $disk, $val, $subid);
                foreach ($numAllArr as $numAllArrVal)
                {
                    $numTempArr = str_split($numAllArrVal);
                    $numIdxTemp = count(array_unique($numTempArr));
                    $numIdxTemp = $SRate_3d->betForNumDiffTbl[$val][$numIdxTemp];
                    $numIdx2Temp = intval($numAllArrVal);
                    if (isset($getRatesNumArr[$numIdx2Temp]))
                    {
                        $rateTemp = floatval($rateAllArr[$numIdxTemp] - $getRatesNumArr[$numIdx2Temp]);
                    }
                    else
                    {
                        $rateTemp = floatval($rateAllArr[$numIdxTemp]);
                    }
                    if (isset($getRatesSNumArr[$numIdx2Temp]))
                    {
                        $rateTemp = floatval($rateTemp - $getRatesSNumArr[$numIdx2Temp]);
                    }
                    $rateOKArr[] = $rateTemp;
                }
            }
            else
            {
                foreach ($numAllArr as $numAllArrVal)
                {
                    $tmpidx = "r" . str_pad($numAllArrVal, 2, "0", STR_PAD_LEFT);
                    if (isset($rateAllArr[$tmpidx]))
                    {
                        $rateOKArr[] = $rateAllArr[$tmpidx];
                    }
                }
            }
            $numArr = implode("\",\"", $numAllArr);
            $rateArr = implode(",", $rateOKArr);
            if ($show5N)
            {
                $tempStr .= "\"r" . $val . "_" . $subid . "\":{\"number\":[\"" . $numArr . "\"],\"rates\":[" . $rateArr . "]}, ";
            }
            else
            {
                $tempStr .= "\"r" . $val . "\":{\"number\":[\"" . $numArr . "\"],\"rates\":[" . $rateArr . "]}, ";
            }
            ++$subid;
        }
    }
    if (substr($tempStr, 0 - 2) == ", ")
    {
        $tempStr = substr($tempStr, 0, strlen($tempStr) - 2);
    }
    $tempStr .= "}";
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo $tempStr;
}
?>